package net.dgg.rz.production.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.rz.common.utils.*;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 报表 controller
 * Created by chenyang
 */
@Controller
@RequestMapping("/report")
public class FinancingProductReportController extends DggBaseController {


    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    private DataPermissionUtils dataPermissionUtils;


    /**
     * @Description 资金业务统计报表路由接口
     * @Author chenyang
     */
    @RequestMapping(value = "/capital_business")
    public String capitalBusiness(Model model) {
        return "rzsc/report/capital_business";
    }

    /**
     * @Description 信用贷款统计报表路由接口
     * @Author chenyang
     */
    @RequestMapping(value = "/credit_loan")
    public String creditLoan(Model model) {
        return "rzsc/report/credit_loan";
    }

    /**
     * @Description 抵押贷款统计报表路由接口
     * @Author chenyang
     */
    @RequestMapping(value = "/mortgage_loan")
    public String mortgageLoan(Model model) {
        return "rzsc/report/mortgage_loan";
    }


    /**
     * @Description 跟进中统计汇总路由接口
     * @Author chenyang
     */
    @RequestMapping(value = "/follow_up_report")
    public String followUpReport(Model model) {
        return "rzsc/report/follow_up_report";
    }

    /**
     * @Description 跟进中统计数据汇总列表数据
     * @Author chenyang
     */
    @RequestMapping(value = "/follow_up_report_data")
    @ResponseBody
    public DggRestResponse followUpReportData(@RequestParam Map<String, Object> params) {

        ValidateUtils.strNotEmpty(params.get("businessName"), ProductOrderExcption.class, "请选择正确的业务类型！");
        try {
            //查询时间
            if (StringUtils.isNotEmpty(String.valueOf(params.get("queryDate")))) {
                String queryDate = String.valueOf(params.get("queryDate"));
                String fromDate = queryDate.substring(0, queryDate.indexOf("~"));
                fromDate = fromDate.trim();
                String toDate = queryDate.substring(queryDate.indexOf("~") + 1);
                toDate = toDate.trim();
                params.put("fromDate", fromDate);
                params.put("toDate", toDate);
            }
            Map<String, Object> resultData = null;

            int businessName = Integer.parseInt(params.get("businessName").toString());
            switch (businessName) {
                case 1:
                    resultData = productOrderService.findMortgageLoanCount(params);
                    break;
                case 2:
                    resultData = productOrderService.findCreditLoanCount(params);
                    break;
                case 3:
                    resultData = productOrderService.findCapitalBusinessCount(params);
                    break;
                case 4:
                    resultData = productOrderService.findBackGroundReportCount(params);
                    break;
                default:
                    break;
            }
            List<Map<String, Object>> followUpReportList = productOrderService.getFollowUpReportWithPage(params);
            return new DataTableResponse().data(params, followUpReportList, resultData);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return getFailResponse("系统异常,请联系管理员!");
        }

    }

    /**
     * @Description 后台生产汇总统计汇总路由接口
     * @Author chenyang
     */
    @RequestMapping(value = "/background_product")
    public String backgroundProduct(Model model) {
        return "rzsc/report/background_product";
    }

    /**
     * @Description 后台生产汇总统计汇总列表数据
     * @Author chenyang
     */
    @RequestMapping(value = "/background_product_data")
    @ResponseBody
    public DggRestResponse backgroundProductData(@RequestParam Map<String,Object> params) {
        try {
            Map<String, Object> resultData = productOrderService.findMortgageLoanCount(params);
            List<Map<String, Object>> followUpReportList = productOrderService.getFollowUpReportWithPage(params);
            return new DataTableResponse().data(params, followUpReportList, resultData);
        } catch (Exception e) {
            return getFailResponse("系统异常,请联系管理员!");
        }
    }

    /**
     * @Description 汇总跟进中统计抵押贷款汇总Excel导出
     * @Author chenyang
     */
    @RequestMapping(value = "/excel_export_mortgage_loan")
    @ResponseBody
    @SuppressWarnings("all")
    public void excelExportMortgageLoan(@RequestParam Map<String, Object> params) {
        int[] widths = new int[]{512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8};
        int[] styleArr = new int[]{-1, -1, -1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, -1, -1, -1};
        List<Map<String, Object>> followUpReportList = productOrderService.getFollowUpReportWithPage(params);
        // dataPermissionUtils.flowUserDataAccess(params, ModuleCodeConstant.RZSC_STATISTICAL_CODE);
        String sheetName = "汇总跟进中抵押贷款统计汇总";
        String[] title = new String[]{"生产事业部", "部门", "姓名", "业务类型", "产品名称", "渠道名称", "办理中", "暂缓中", "订单已超期", "匹配渠道", "预签申请合同", "已报审", "审核通过", "通过金额", "结案注销", "已办抵押"};
        String[] keyArray = new String[]{"productOrgName", "flowUserOrgName", "flowUserName", "businessProductName", "productName", "channelName", "handling", "reprieve", "overdue", "channel", "preContract", "totalReporting", "auditApproval", "throughAmount", "writeOffAmount", "mortgageAmount"};
        ExcelTool.execlExportMap(followUpReportList, sheetName, title, keyArray, widths, styleArr);
    }

    /**
     * @Description 汇总跟进中统计信用贷款汇总Excel导出
     * @Author chenyang
     */
    @RequestMapping(value = "/excel_export_credit_loan")
    @ResponseBody
    @SuppressWarnings("all")
    public void excelExportCreditLoan(@RequestParam Map<String, Object> params) {
        int[] widths = new int[]{512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8};
        int[] styleArr = new int[]{-1, -1, -1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, -1};
        List<Map<String, Object>> followUpReportList = productOrderService.getFollowUpReportWithPage(params);
        // dataPermissionUtils.flowUserDataAccess(params, ModuleCodeConstant.RZSC_STATISTICAL_CODE);
        String sheetName = "汇总跟进中信用贷款统计汇总";
        String[] title = new String[]{"生产事业部", "部门", "姓名", "业务类型", "产品名称", "渠道名称", "办理中", "暂缓中", "订单超期", "匹配渠道", "完善资料", "报审", "审核通过", "通过金额"};
        String[] keyArray = new String[]{"productOrgName", "flowUserOrgName", "flowUserName", "businessProductName", "productName", "channelName", "handling", "reprieve", "overdue", "channel", "completeMaterial", "totalReporting", "auditApproval", "throughAmount"};
        ExcelTool.execlExportMap(followUpReportList, sheetName, title, keyArray, widths, styleArr);
    }

    /**
     * @Description 汇总跟进中统计资金业务汇总Excel导出
     * @Author chenyang
     */
    @RequestMapping(value = "/excel_export_capital_business")
    @ResponseBody
    @SuppressWarnings("all")
    public void excelExportCapitalBusiness(@RequestParam Map<String, Object> params) {
        int[] widths = new int[]{512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8};
        int[] styleArr = new int[]{-1, -1, -1, 1, 1, 0};
        List<Map<String, Object>> followUpReportList = productOrderService.getFollowUpReportWithPage(params);
       // dataPermissionUtils.flowUserDataAccess(params, ModuleCodeConstant.RZSC_STATISTICAL_CODE);
        String sheetName = "汇总跟进中资金业务统计汇总";
        String[] title = new String[]{"生产事业部", "部门", "姓名", "业务类型", "办理中", "已暂缓",};
        String[] keyArray = new String[]{"productOrgName", "flowUserOrgName", "flowUserName", "businessProductName", "handling", "reprieve"};
        ExcelTool.execlExportMap(followUpReportList, sheetName, title, keyArray, widths, styleArr);
    }

    /**
     * @Description 汇总跟进中统计资金业务汇总Excel导出
     * @Author chenyang
     */
    @RequestMapping(value = "/excel_export_background_product")
    @ResponseBody
    @SuppressWarnings("all")
    public void excelExportBackground(@RequestParam Map<String, Object> params) {
        int[] widths = new int[]{512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8, 512 * 8};
        int[] styleArr = new int[]{-1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1};
        List<Map<String, Object>> followUpReportList = productOrderService.getFollowUpReportWithPage(params);
        // dataPermissionUtils.flowUserDataAccess(params, ModuleCodeConstant.RZSC_STATISTICAL_CODE);
        String sheetName = "汇总跟进中资金业务统计汇总";
        String[] title = new String[]{"生产事业部", "部门", "姓名", "业务类型", "产品名称", "渠道名称", "接单量", "退单量", "废单量", "放款金额", "放款笔数", "扣罚操行分", "扣罚金额"};
        String[] keyArray = new String[]{"productOrgName", "flowUserOrgName", "flowUserName", "businessProductName", "productName", "channelName", "receiveCount", "returnCount", "discardCount", "loanAmount", "loanCount", "deductionPenalty", "deductionMoney"};
        ExcelTool.execlExportMap(followUpReportList, sheetName, title, keyArray, widths, styleArr);
    }


}
